package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    //根据手机号查询会员
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    //新增会员
    public void add(Member member) {
         memberDao.add(member);
    }

    @Override
    public Map<String, Object> getMemberReport() {
        //获取最近一年的月份
        List<String> months = DateUtils.getThisYearMonths();
        //创建存储每月会员人数的集合
        List<Long> memberCount = new ArrayList<>();
        //获取每个月对应的会员总数
        for (String month : months) {
            month += "-01";
            String thisMonthLastDay = DateUtils.parseDate2String(DateUtils.getThisMonthLastDay(month));
            long count = memberDao.getMemberCountByMonth(thisMonthLastDay);
            memberCount.add(count);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("months" , months);
        resultMap.put("memberCount" , memberCount);
        return resultMap;
    }

    @Override
    public List<Integer> findCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<Integer>(months.size());
        for (String month : months) {
            //month 2020-02
            //SELECT COUNT(*) FROM t_member WHERE regTime <= '2020-02-31'
            Integer count = memberDao.findCountByMonth(month + "-1");
            memberCounts.add(count);
        }
        return memberCounts;
    }

    @Override
    public List<Map<String, Object>> getMemberSexReport() {
        return memberDao.getMemberSexReport();
    }
    @Override
    public List<Map<String, Object>> getMemberAgeReport() {
        return memberDao.getMemberAgeReport();
    }
}


